function getDepartments(nombre, callback){
    jQuery.noConflict();
    jQuery.ajax({
        url:"index.php?option=com_acaweb&controller=PublicData.Locaciones&task=GetDepartamento&NombrePais=" + nombre + "&ajax=true",
        type: "GET",
        dataType:"json",
        success: callback
    });
}

function getMunicipality(department, callback){
    jQuery.noConflict();
    jQuery.ajax({
        url:"index.php?option=com_acaweb&controller=PublicData.Locaciones&task=GetMunicipio&CodigoDepartamento=" + department + "&ajax=true",
        type: "GET",
        dataType:"json",
        success: callback
    });
}

function loadDepartmentsForCoutry(data){
    var options = "<option> - Elegir - </option>";
    jQuery("#municipio-nacimiento").html("");

    for(var i in data){
        if(typeof data[i] != "function") {
            options += '<option value="' + data[i].CodigoDepartamento + '">';
            options += data[i].Nombre;
            options += '</options>';
        }
    }
    jQuery("#departamento-nacimiento").html(options);
    return false;
}

function loadDepartments(element, formatCallback){
    getDepartments(function(departments){
        for (var i in departments) {
            var child;
            
            if(typeof departments[i] != "function"){
                if(typeof formatCallback == "function"){
                    child = formatCallback(departments[i]);
                }else{
                    child = '<option value="'+ departments[i].CodigoDepartamento +'">'+ departments[i].Nombre +'</option>';
                }
            }

            jQuery(element).append(child);
        }
    });
}

function onDepartmentSelect(departMentSelector, citiesHolder, formatCallback, departmentNameHolder){

    jQuery(departMentSelector).change(function(){
        var departmentId = jQuery(departMentSelector + " option:selected").val();
        var departmentName = jQuery(departMentSelector + " option:selected").text();
        if(typeof departmentNameHolder != "undefined"){
            jQuery(departmentNameHolder).val(departmentName);
        }
        
        getMunicipality(departmentId, function(cities){
            
            var options = "<option> - Elegir - </option>";
            for (var i in cities) {
                if(typeof cities[i] != "function"){
                    if(typeof formatCallback == "function"){
                        options += formatCallback(cities[i]);
                    }else{
                        options += '<option value="'+ cities[i].CodigoMunicipio +'">'+ cities[i].Nombre +'</option>';
                    }
                }
            }
            jQuery(citiesHolder).html(options);
        });
    });
}

function prepareCourses(){
    jQuery(document).ready(function(){
        jQuery.noConflict();
        jQuery(".tipo").click(function(){
            jQuery("#detalleConcepto").val(this.id);
        });
        jQuery("#inscription-form").hide();
        jQuery("#careers").show();
        jQuery("#tabs").tabs({
            load:function(){
                jQuery(".career").click(function(){
                    jQuery("#codigoPlan").val(this.id);
                    jQuery("#nombrePlan").val(this.text);
                    jQuery("#career-name").html(this.text);
                    jQuery("#careers").hide();
                    jQuery("#inscription-form").show();
                    jQuery(".notification").click();
                });
            },
            ajaxOptions: {
                dataFilter: function(jsonText){
                    var data = jQuery.parseJSON(jsonText);
                    var result = "<ul class='dash'>";

                    for(var i in data){
                        if(typeof data[i] != "function"){
                            result += '<li class="fade_hover" style="width:150px;">';
                            result += '<a class="career" id="' + data[i].CodigoPlan + '" href="#" style="width:150px;">';
                            result += data[i].NombrePlan;
                            result += '</a></li>'
                        }
                    }

                    result += '</ul>';
                    
                    return result;
                }
            }
        });
    });
}
